package com.rongshu.view.home.fragment.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.home.activity.RecommendSupportActivity;
import com.utils.DensityUtil;
import com.widget.toast.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecommendWayDialog extends Dialog {
    @BindView(R.id.edt_recommend)
    EditText edt_recommend;
    @BindView(R.id.txt_small_card)
    TextView txt_small_card;
    @BindView(R.id.txt_big_card)
    TextView txt_big_card;
    @BindView(R.id.txt_supper_card)
    TextView txt_supper_card;
    @BindView(R.id.txt_card)
    TextView txt_card;
    @BindView(R.id.iv_rule)
    ImageView iv_rule;
    @BindView(R.id.txt_hint)
    TextView txt_hint;
    @BindView(R.id.btn_confirm)
    Button btn_confirm;

    public RecommendWayDialog(Context context) {
        super(context, R.style.custom_dialog2);
        View view = View.inflate(context, R.layout.fragment_recommend_way_dialog_view, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        int screenWidth = DensityUtil.getScreenWidth(context);
        layoutParams.width = screenWidth - 60;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        setCanceledOnTouchOutside(true);
    }

    public static class Builder implements View.OnClickListener {
        private Context context;
        private RecommendWayDialog dialog;
        //推荐卡数量
        private int propsCount = -1;
        //记录选中的是哪个
        private int select_card = -1;
        //boolean
        private boolean isClick = false;
        private String[] peopleList = new String[]{"300", "2000", "10000"};
        private String[] leafList = new String[]{"300", "1000", "5000"};

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置道具数量
         */
        public Builder setPropsCount(int propsCount) {
            this.propsCount = propsCount;
            return this;
        }

        public RecommendWayDialog create() {
            if (dialog == null) {
                dialog = new RecommendWayDialog(context);
            }
            dialog.txt_card.setText("推荐卡(" + propsCount + ")");
            dialog.txt_small_card.setOnClickListener(this);
            dialog.txt_big_card.setOnClickListener(this);
            dialog.txt_supper_card.setOnClickListener(this);
            dialog.txt_card.setOnClickListener(this);
            dialog.btn_confirm.setOnClickListener(this);
            dialog.iv_rule.setOnClickListener(this);
            dialog.edt_recommend.setOnClickListener(this);
            return dialog;
        }

        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.txt_small_card:
                    select_card = (select_card == 1) ? -1 : 1;
                    selectCardRecommend();
                    break;
                case R.id.txt_big_card:
                    select_card = (select_card == 2) ? -1 : 2;
                    selectCardRecommend();
                    break;
                case R.id.txt_supper_card:
                    select_card = (select_card == 3) ? -1 : 3;
                    selectCardRecommend();
                    break;
                case R.id.txt_card:
                    select_card = (select_card == 4) ? -1 : 4;
                    selectCardRecommend();
                    break;
                case R.id.btn_confirm://确认推荐
                    int recommend_type = select_card;
                    int pay_type = (select_card == 4) ? 2 : 1;//1使用榕树叶，2使用推荐卡
                    String contents = null;
                    if (select_card == -1) {
                        ToastUtils.show("请选择推荐方式");
                        return;
                    } else if (select_card == 4) {
                        if (propsCount == 0 || propsCount == -1) {
                            ToastUtils.show("没有可用的推荐卡");
                            return;
                        } else {
                            recommend_type = 1;
                            pay_type = 2;
                        }
                    }
                    contents = dialog.edt_recommend.getText().toString();
                    if (TextUtils.isEmpty(contents)) {
                        ToastUtils.show("请输入推荐语");
                        return;
                    }

//                if (getActivity() != null && getActivity() instanceof SingleArticleDetailsActivity) {
//                    ((SingleArticleDetailsActivity) getActivity()).postRecommend(recommend_type, pay_type, contents);
//                }

//                if (homeFragment != null) {
//                    homeFragment.postRecommend(recommend_type, pay_type, contents);
//                }
                    break;
                case R.id.iv_rule:
                    Intent intent = new Intent(context, RecommendSupportActivity.class);
                    context.startActivity(intent);
                    break;
                case R.id.edt_recommend:
                    isClick = true;
                    break;
            }
        }

        /**
         * 选择推荐
         */
        private void selectCardRecommend() {
            dialog.txt_small_card.setBackgroundResource(select_card == 1 ?
                    R.drawable.shape_commend_card_select_bg :
                    R.drawable.shape_commend_card_unselect_bg);
            dialog.txt_small_card.setTextColor(select_card == 1 ?
                    ContextCompat.getColor(context, R.color.color_3cc051)
                    : ContextCompat.getColor(context, R.color.color_4a4a4a));
            dialog.txt_big_card.setBackgroundResource(select_card == 2 ?
                    R.drawable.shape_commend_card_select_bg :
                    R.drawable.shape_commend_card_unselect_bg);
            dialog.txt_big_card.setTextColor(select_card == 2 ?
                    ContextCompat.getColor(context, R.color.color_3cc051)
                    : ContextCompat.getColor(context, R.color.color_4a4a4a));
            dialog.txt_supper_card.setBackgroundResource(select_card == 3 ?
                    R.drawable.shape_commend_card_select_bg :
                    R.drawable.shape_commend_card_unselect_bg);
            dialog.txt_supper_card.setTextColor(select_card == 3 ?
                    ContextCompat.getColor(context, R.color.color_3cc051)
                    : ContextCompat.getColor(context, R.color.color_4a4a4a));
            dialog.txt_card.setBackgroundResource(select_card == 4 ?
                    R.drawable.shape_commend_card_select_bg :
                    R.drawable.shape_commend_card_unselect_bg);
            dialog.txt_card.setTextColor(select_card == 4 ?
                    ContextCompat.getColor(context, R.color.color_3cc051)
                    : ContextCompat.getColor(context, R.color.color_4a4a4a));
            dialog.txt_hint.setText(getHint());
            dialog.btn_confirm.setBackgroundResource(select_card != -1 ?
                    R.drawable.shape_recommend_confirm_green_bg :
                    R.drawable.shape_recommend_confirm_gray_bg);
            dialog.btn_confirm.setTextColor(select_card != -1 ?
                    ContextCompat.getColor(context, R.color.white) :
                    ContextCompat.getColor(context, R.color.color_4a4a4a));
        }

        private String getHint() {
            String hint = null;
            if (select_card < 4 && select_card > 0) {
                String people = peopleList[select_card - 1];
                String leaf = leafList[select_card - 1];
                hint = "本篇文章将推荐给" + people + "人阅读，需要使用" + leaf + "榕树叶";
            } else if (select_card == 4) {
                String people = peopleList[0];
                String leaf = leafList[0];
                hint = "本篇文章将推荐给" + people + "人阅读，需要使用" + leaf + "榕树叶";
            }
            return hint;
        }
    }
}
